package org.giovanolli.mumi.view.interceptor;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.giovanolli.mumi.view.util.FacesContextUtils;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MessageResponseViewInterceptor {

	private static final String PACKAGE_TO_LISTEN = "org.giovanolli.mumi.dao";

	private static final String PACKAGE_TO_LISTEN2 = "org.giovanolli.mumi.controller";

	private static final String MESSAGE_KEY_SAVED = "action.status.saved";

	@AfterReturning(pointcut = "execution(* " + PACKAGE_TO_LISTEN + ".*.*.save(..))", returning = "result")
	public void logAfterReturning(JoinPoint joinPoint, Object result) {
		FacesContextUtils.addMessageToContext(MESSAGE_KEY_SAVED);
	}

	@AfterReturning(pointcut = "execution(* " + PACKAGE_TO_LISTEN2 + ".*.*.confirmSave(..))", returning = "result")
	public void logAfterReturning2(JoinPoint joinPoint, Object result) {
		FacesContextUtils.addMessageToContext(MESSAGE_KEY_SAVED);
	}

	@AfterThrowing(pointcut = "execution(* " + PACKAGE_TO_LISTEN + ".*.*.delete(..))")
	public void logAfterThrowing(JoinPoint joinPoint) {
		FacesContextUtils.addMessageToContext(MESSAGE_KEY_SAVED);
	}

}
